package com.xuexuehai.carrecommand.service.impl;

import com.xuexuehai.carrecommand.dao.ICarDao;
import com.xuexuehai.carrecommand.entity.Car;
import com.xuexuehai.carrecommand.service.ICarService;
import com.xuexuehai.carrecommand.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuexuehai
 * @mailBox xuehai.xue@QQ.com
 * @date 2021/8/22 10:16 下午
 */
@Service
@Transactional
public class CarServiceImpl implements ICarService {
    @Autowired
    private ICarDao iCarDao;

    @Override
    public List<Car> findAllCar() {
        return iCarDao.findAll();
    }

    @Override
    public void addCar(Car car) {
        iCarDao.save(car);
    }

    @Override
    public List<CarVo> findCarFactory() {
        Object[] objs = iCarDao.countCarFactory();

        List<CarVo> cvs = new ArrayList<>();

        for(Object obj : objs){
            Object[] carFactoryNum = (Object[])obj;
            CarVo carVo = new CarVo(carFactoryNum[1],carFactoryNum[0]);
            cvs.add(carVo);
        }

        return cvs;
    }

}
